package [path_1].[path_2].[path_3].service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import [path_1].[path_2].[path_3].mapper.[Table2]Mapper;
import [path_1].[path_2].entity.PageResult;
import [path_1].[path_2].pojo.[path_3].[Table2];
import [path_1].[path_2].dto.[path_3].[Table2]Dto;
import [path_1].[path_2].service.[path_3].[Table2]Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class [Table2]ServiceImpl implements [Table2]Service {

    private final [Table2]Mapper [table2]Mapper;

    /**
     * 返回全部记录
     * @return
     */
	@Override
    public List<[Table2]> get[Table2]All() {
        return [table2]Mapper.selectAll();
    }

    /**
     * 分页+条件查询
     * @param dto
     * @return
     */
	@Override
    public PageResult get[Table2]List([Table2]Dto dto) {
        Example example = createExample(dto);
        Page<Object> pageContent = PageHelper.startPage(dto.getPage() - 1, dto.getSize()).setOrderBy("id asc")
                .doSelectPage(() -> [table2]Mapper.selectByExample(example));
        PageResult result = PageResult.builder()
                .total(pageContent.getTotal())
                .rows(pageContent.getResult())
                .build();
        return result;
    }

    /**
     * 根据Id查询
     * @param [key]
     * @return
     */
	@Override
    public [Table2] get[Table2]ById([keyType] [key2]) {
        return [table2]Mapper.selectByPrimaryKey([key2]);
    }

    /**
     * 新增
     * @param dto
     */
	@Override
	@Transactional
    public void save[Table2]([Table2]Dto dto) {
		[Table2] [table2] = new [Table2]();
        BeanUtils.copyProperties(dto, [table2]);
        [table2].setId(null);
        [table2]Mapper.insertSelective([table2]);
    }

    /**
     * 修改
     * @param dto
     */
	@Override
	@Transactional
    public void update[Table2]([Table2]Dto dto) {
		[Table2] [table2] = new [Table2]();
        BeanUtils.copyProperties(dto, [table2]);
        [table2]Mapper.updateByPrimaryKeySelective([table2]);
    }

    /**
     *  删除
     * @param [key]
     */
	@Override
	@Transactional
    public void delete[Table2]ById([keyType] [key2]) {
        [table2]Mapper.deleteByPrimaryKey([key2]);
    }

    /**
     * 构建查询条件
     * @param dto
     * @return
     */
    private Example createExample([Table2]Dto dto){
        Example example=new Example([Table2].class);
        Example.Criteria criteria = example.createCriteria();
        if(null != dto){
			<条件查询.String.txt>
			<条件查询.Integer.txt>
        }
        return example;
    }

}
